﻿/*
 * Created by SharpDevelop.
 * User: christophe
 * Date: 12/03/2011
 * Time: 14:32
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.IO;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace MetaVideoEditorEvolved.Converter
{
	/// <summary>
	/// Description of ImagePathToSizeConver.
	/// </summary>
	public class ImagePathToSizeConverter : IValueConverter
	{
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			string path = value as string;
			if (string.IsNullOrEmpty(path))
			{
				return null;
			}

			var localPath = mveEngine.ImageUtil.GetLocalImagePath(path);

			if (string.IsNullOrEmpty(localPath))
			{
				return null;
			}

			try
            {
                System.Drawing.Bitmap img = new System.Drawing.Bitmap(localPath);

                if (parameter == null)
                {
                    return img.Size;
                }

                if (parameter.ToString() == "Width")
                {
                    return img.Size.Width.ToString();
                }
                else
                {
                    if (parameter.ToString() == "Height")
                    {
                        return img.Size.Height.ToString();
                    }
                    else
                    {
                    	if(parameter.ToString() == "Ratio")
                    	{
                    		return Math.Round((float)img.Size.Width / (float)img.Size.Height,2).ToString();
                    	}
                    	else
                    	{
                        	return img.Size;
                    	}
                    }
                }
            }
            catch (ArgumentException)
            {
                return null;
            }
            catch(Exception ex)
            {
            	throw ex;
            }
		}

		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			throw new NotImplementedException();
		}
	}
}
